package com.hc.daoimp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hc.dao.RepariDao;
import com.hc.entity.House;
import com.hc.entity.Logistics;
import com.hc.entity.Repair;
import com.hc.util.JDBCUtil;

public class RepairDaoImpl implements RepariDao {
	@Override
	public List<House> getallhouse() {
		ArrayList<House> arrayList = new ArrayList<>();
		JDBCUtil.sql="select * from house";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while(JDBCUtil.rs.next()){
				House house = new House();
				house.setHid(JDBCUtil.rs.getInt(1));
				house.setHaddress(JDBCUtil.rs.getString(2));
				house.setHcommunity(JDBCUtil.rs.getString(3));
				house.setHunit(JDBCUtil.rs.getInt(4));
				house.setHfloor(JDBCUtil.rs.getInt(5));
				house.setHnum(JDBCUtil.rs.getInt(6));
				house.setHarea(JDBCUtil.rs.getInt(7));
				house.setHtoward(JDBCUtil.rs.getString(8));
				house.setHdecorate(JDBCUtil.rs.getString(9));
				house.setHshaungqi(JDBCUtil.rs.getString(10));
				house.setHlimit(JDBCUtil.rs.getInt(11));
				house.setHfacilities(JDBCUtil.rs.getString(12));
				house.setHprice(JDBCUtil.rs.getInt(13));
				house.setHstate(JDBCUtil.rs.getString(14));
				house.setHmessage(JDBCUtil.rs.getString(15));
				house.setHaddtime(JDBCUtil.rs.getString(16));
				house.setHupdatetime(JDBCUtil.rs.getString(17));
				arrayList.add(house);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
	}

	@Override
	public List<Logistics> getalllogistics() {
		ArrayList<Logistics> arrayList = new ArrayList<>();
		JDBCUtil.sql="SELECT * FROM logistics";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while(JDBCUtil.rs.next()){
				Logistics logistics=null;
				int lid = JDBCUtil.rs.getInt(1);
				String lname = JDBCUtil.rs.getString(2);
				String lnumber = JDBCUtil.rs.getString(3);
				int lphone = JDBCUtil.rs.getInt(4);
				String lgender = JDBCUtil.rs.getString(5);
				String ldepartment = JDBCUtil.rs.getString(6);
				String laddtime = JDBCUtil.rs.getString(7);
				int lwage = JDBCUtil.rs.getInt(8);
				String laddress = JDBCUtil.rs.getString(9);
				logistics=new Logistics(lid, lname, lnumber, lphone, lgender, ldepartment, laddtime, lwage, laddress);
				arrayList.add(logistics);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
	}

	@Override
	public void add(Repair repair) {
		JDBCUtil.sql="INSERT INTO `repair` VALUES(null,?,?,?,?,?,?)";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, repair.getRpmessage());
			JDBCUtil.ps.setString(2, repair.getRpdate());
			JDBCUtil.ps.setString(3, repair.getReresult());
			JDBCUtil.ps.setString(4, repair.getRpnote());
			JDBCUtil.ps.setInt(5, repair.getLogistics().getLid());
			JDBCUtil.ps.setInt(6, repair.getHouse().getHid());
			
			JDBCUtil.ps.executeLargeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<Repair> select(int id, String date) {
		ArrayList<Repair> arrayList = new ArrayList<>();
		JDBCUtil.sql="SELECT * from  `repair`,house,logistics WHERE `repair`.hid=house.hid "
				+ "AND `repair`.lid=logistics.lid AND `repair`.hid=? AND rpdate LIKE ?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.ps.setString(2, "%"+date+"%");
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while(JDBCUtil.rs.next()){
				String rpid	 = JDBCUtil.rs.getString(1);
				String rpmessage = JDBCUtil.rs.getString(2);
				String rpdate = JDBCUtil.rs.getString(3);
				String rpresult = JDBCUtil.rs.getString(4);
				String rpnote = JDBCUtil.rs.getString(5);
				String haddress = JDBCUtil.rs.getString(22);
				String lname = JDBCUtil.rs.getString(26);
				Repair repair = new Repair(Integer.parseInt(rpid), rpmessage, rpdate, rpresult, rpnote, 
						new House(0, null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, haddress, null, null), 
						new Logistics(0, lname, null, 0, null, null, null, 0, null));
				
				arrayList.add(repair);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
	}

	//编辑
	@Override
	public void update(Repair repair) {
		JDBCUtil.sql="UPDATE `repair` SET rpmessage=?,rpdate=?,rpresult=?,rpnote=?,lid=?,hid=? WHERE rpid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, repair.getRpmessage());
			JDBCUtil.ps.setString(2, repair.getRpdate());
			JDBCUtil.ps.setString(3, repair.getReresult());
			JDBCUtil.ps.setString(4, repair.getRpnote());
			JDBCUtil.ps.setInt(5, repair.getLogistics().getLid());
			JDBCUtil.ps.setInt(6, repair.getHouse().getHid());
			JDBCUtil.ps.setInt(7, repair.getRpid());
			JDBCUtil.ps.executeUpdate();
			 
			System.out.println("更新成功");
			JDBCUtil.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//数据回显
	@Override
	public Repair show(int id) {
		Repair repair=null;
		JDBCUtil.sql="SELECT * from  `repair` where rpid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while (JDBCUtil.rs.next()) {
				int rpid = JDBCUtil.rs.getInt(1);
				String rpmessage = JDBCUtil.rs.getString(2);
				String rpdate = JDBCUtil.rs.getString(3);
				String reresult = JDBCUtil.rs.getString(4);
				String rpnote = JDBCUtil.rs.getString(5);
				int lid = JDBCUtil.rs.getInt(6);
				int hid = JDBCUtil.rs.getInt(7);
				
				repair=new Repair(rpid, rpmessage, rpdate, reresult, rpnote, 
						new House(hid, null, null, 0, 0, 0, 0, null, null, null, 0, null, 0, null, null, null, null), 
						new Logistics(lid, null, null, 0, null, null, null, 0, null));
			}
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return repair;
	}
	//删除
	@Override
	public void delete(int id) {
		JDBCUtil.sql="delete from repair where rpid= ?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.ps.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	

	
}
